package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglDtsblist;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglDtsblistMapper;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglDtsblistService;
import org.jeecg.modules.demo.znnqjc.service.impl.DklistServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * @Description: 大田设备列表
 * @Author: jeecg-boot
 * @Date: 2023-04-25
 * @Version: V1.0
 */
@Service
public class ZwglDtsblistServiceImpl extends ServiceImpl<ZwglDtsblistMapper, ZwglDtsblist> implements IZwglDtsblistService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DklistServiceImpl dklistService;
    @Autowired
    private ZwglDtsblistMapper zwglDtsblistMapper;

    @Override
    public String getdksblist() {
        zwglDtsblistMapper.delete(null);
        dklistService.getToken();

        String[] arr;
        arr = new String[15];
        arr[0] = "103270";
        arr[1] = "102870";
        arr[2] = "103275";
        arr[3] = "103277";
        arr[4] = "103276";
        arr[5] = "103278";
        arr[6] = "103279";
        arr[7] = "103280";
        arr[8] = "103281";
        arr[9] = "103282";
        arr[10] = "103283";
        arr[11] = "103286";
        arr[12] = "103285";
        arr[13] = "103284";
        arr[14] = "103287";
        for (int i = 0; i < arr.length; i++) {
            String a = arr[i];
            try {
                String token = (String) redisTemplate.opsForValue().get("nstoken");
//                System.out.println(token);
                String url = "http://112.102.225.89:51012/farming/openapi/getMassDeviceData?massid=" + a;
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("accessToken", token);
                int responseCode = con.getResponseCode();

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                JSONObject jsonObject = JSON.parseObject(String.valueOf(response));
                JSONArray jsonArray = new JSONArray(jsonObject.getJSONArray("data"));
                for (int j = 0; j <= jsonArray.size(); j++) {
                    JSONObject jsonObject2 = jsonArray.getJSONObject(j);
                    ZwglDtsblist zwglDtsblist = new ZwglDtsblist();
                    if (a.equals("103270")) {
                        zwglDtsblist.setDkmc("核心地块1");
                    } else if (a.equals("102870")) {
                        zwglDtsblist.setDkmc("核心地块2");
                    } else if (a.equals("103275")) {
                        zwglDtsblist.setDkmc("核心地块3");
                    } else if (a.equals("103277")) {
                        zwglDtsblist.setDkmc("核心地块4");
                    } else if (a.equals("103276")) {
                        zwglDtsblist.setDkmc("核心地块5");
                    } else if (a.equals("103278")) {
                        zwglDtsblist.setDkmc("核心地块6");
                    } else if (a.equals("103279")) {
                        zwglDtsblist.setDkmc("旱田地块1");
                    } else if (a.equals("103280")) {
                        zwglDtsblist.setDkmc("旱田地块2");
                    } else if (a.equals("103281")) {
                        zwglDtsblist.setDkmc("旱田地块3");
                    } else if (a.equals("103282")) {
                        zwglDtsblist.setDkmc("地块10");
                    } else if (a.equals("103283")) {
                        zwglDtsblist.setDkmc("地块11");
                    } else if (a.equals("103286")) {
                        zwglDtsblist.setDkmc("地块12");
                    } else if (a.equals("103285")) {
                        zwglDtsblist.setDkmc("地块13");
                    } else if (a.equals("103284")) {
                        zwglDtsblist.setDkmc("地块16");
                    } else if (a.equals("103287")) {
                        zwglDtsblist.setDkmc("地块18");
                    }
                    zwglDtsblist.setSbbh(jsonObject2.getString("code"));
                    zwglDtsblist.setSbmc(jsonObject2.getString("fullname"));
                    if (jsonObject2.getString("category").equals("05")) {
                        zwglDtsblist.setSblx("气象站");
                    } else {
                        zwglDtsblist.setSblx("墒情仪");
                    }
                    if (jsonObject2.getString("fullname").length() > 4) {
                        String subStr = jsonObject2.getString("fullname").substring(0, 4);
                        if (subStr.equals("气象站1")) {
                            zwglDtsblist.setDkbh("20");
                        } else if (subStr.equals("气象站2")) {
                            zwglDtsblist.setDkbh("21");
                        } else if (subStr.equals("气象站3")) {
                            zwglDtsblist.setDkbh("22");
                        } else if (subStr.equals("气象站4")) {
                            zwglDtsblist.setDkbh("23");
                        } else if (subStr.equals("气象站5")) {
                            zwglDtsblist.setDkbh("24");
                        } else if (subStr.equals("气象站6")) {
                            zwglDtsblist.setDkbh("25");
                        } else if (subStr.equals("墒情站1")) {
                            zwglDtsblist.setDkbh("8");
                        } else if (subStr.equals("墒情站2")) {
                            zwglDtsblist.setDkbh("9");
                        } else if (subStr.equals("墒情站3")) {
                            zwglDtsblist.setDkbh("10");
                        } else if (subStr.equals("墒情站4")) {
                            zwglDtsblist.setDkbh("11");
                        } else if (subStr.equals("墒情站5")) {
                            zwglDtsblist.setDkbh("12");
                        } else if (subStr.equals("墒情站6")) {
                            zwglDtsblist.setDkbh("13");
                        } else if (subStr.equals("墒情站7")) {
                            zwglDtsblist.setDkbh("14");
                        } else if (subStr.equals("墒情站8")) {
                            zwglDtsblist.setDkbh("15");
                        } else if (subStr.equals("墒情站9")) {
                            zwglDtsblist.setDkbh("16");
                        } else if (subStr.equals("墒情站10")) {
                            zwglDtsblist.setDkbh("17");
                        } else if (subStr.equals("墒情站11")) {
                            zwglDtsblist.setDkbh("18");
                        } else if (subStr.equals("墒情站12")) {
                            zwglDtsblist.setDkbh("19");
                        }

                    }

                    zwglDtsblistMapper.insert(zwglDtsblist);
                    getsbwz();


                }


            } catch (Exception e) {
                System.out.println("Exception: " + e.getMessage());
            }

        }
        return "ok";
    }

    public String getsbwz() {
        dklistService.getToken();
        try {
            String token = (String) redisTemplate.opsForValue().get("nstoken");

            String url = "http://112.102.225.89:51012/farming/openapi/getDeviceData";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("accessToken", token);
            int responseCode = con.getResponseCode();

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JSONObject jsonObject = JSON.parseObject(String.valueOf(response));

            JSONArray jsonArray = new JSONArray(jsonObject.getJSONArray("data"));
            for (int j = 0; j <= jsonArray.size(); j++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(j);
                ZwglDtsblist zwglDtsblist = new ZwglDtsblist();
                QueryWrapper<ZwglDtsblist> queryWrapper = new QueryWrapper<>();
                queryWrapper.select("*");
                List<ZwglDtsblist> list = zwglDtsblistMapper.selectList(queryWrapper);
                for (ZwglDtsblist zw : list) {
                    if (zw.getSbmc().equals(jsonObject2.getString("fullname"))) {
                        if (jsonObject2.getString("coordinates") == null) {
                            zw.setJd("0");
                            zw.setWd("0");
                        } else {
                            String coordinates = jsonObject2.getString("coordinates");
                            String[] parts = coordinates.split(",");
                            String longitude = parts[0];
                            String latitude = parts[1];
                            // 将经度和纬度存储到相应的字段中
                            zw.setJd(longitude);
                            zw.setWd(latitude);
                            zwglDtsblistMapper.updateById(zw);
                        }

                    }


                }

            }


        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }


        return "ok";
    }

    @Override
    public Result qxz(String dkmc) {
        QueryWrapper<ZwglDtsblist> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sbmc", "气象");
        queryWrapper.eq("dkmc", dkmc);
        List<ZwglDtsblist> list = zwglDtsblistMapper.selectList(queryWrapper);
        return Result.OK(list);
    }


}
